//
//  ApplicationContext.m
//  Finder
//
//  Created by cdm on 11-3-19.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "ApplicationContext.h"
#import "Content.h"
#import "Menu.h"

static ApplicationContext* instance;

@interface ApplicationContext ()

- (void)loadContents;
- (void)loadAds;
- (void)loadRecommendApps;
- (void)loadAppSetting;

@end

@implementation ApplicationContext

@synthesize contents;
@synthesize ads;
@synthesize recommendApps;
@synthesize appSetting;

+ (ApplicationContext *)sharedContext 
{
	@synchronized(self)
	{
		if  (instance  ==  nil)
		{
            instance = [[self alloc] init];
        }
    }
	
	return  instance;
}


+ (id)allocWithZone:(NSZone *)zone 
{
    @synchronized(self) {
        if (instance == nil) {
            instance= [super allocWithZone:zone];
            return instance;  // assignment and return on first allocation
        }
    }
    return nil; // on subsequent allocation attempts return nil
}

- (id)init {
    self = [super init];
	if (self) {
        [self loadContents];
        [self loadAds];
        [self loadRecommendApps];
        [self loadAppSetting];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadData) name:kAppLanguageChangedNotification object:nil];
	}
	
	return self;
}

- (void)dealloc 
{
    [self.contents release];
    [self.ads release];
    [self.recommendApps release];
    [self.appSetting release];
	[super dealloc];
}

- (id)copyWithZone:(NSZone *)zone
{
    return self;
}

- (id)retain 
{
    return self;
}

- (unsigned)retainCount 
{
    return UINT_MAX;  // denotes an object that cannot be released
}

- (void)release 
{
    //do nothing
}

- (id)autorelease 
{
    return self;
}

#pragma mark - Load data

- (void)loadContents
{
    NSMutableArray *data = [Content contentsFromPlist:@"ContentData.plist"];
    self.contents = data;
}

- (void)loadAds
{
    NSMutableArray *data = [Content contentsFromPlist:@"AdData.plist"];
    self.ads = data;
}

- (void)loadRecommendApps
{
    NSMutableArray *data = [Content contentsFromPlist:@"RecommendAppData.plist"];
    self.recommendApps = data;
}

- (void)loadAppSetting
{
    AppSetting *setting = [AppSetting settingFromPlist:@"AppSetting.plist"];
    self.appSetting = setting;
}

- (void)reloadData
{
    [self loadContents];
    [self loadAds];
    [self loadRecommendApps];
    [self loadAppSetting];
}

@end
